package com.cqrt.util;

import ma.glasnost.orika.MapperFacade;
import ma.glasnost.orika.MapperFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

@Component("customBeanMappers")
public class BeanMappers {

    private final MapperFacade mapperFacade;

    @Autowired
    public BeanMappers(MapperFactory mapperFactory) {
        this.mapperFacade = mapperFactory.getMapperFacade();
    }

    public <S, D> D map(S source, Class<D> destinationClass) {
        if (source == null) {
            return null;
        }
        return mapperFacade.map(source, destinationClass);
    }

    public <S, D> List<D> mapList(List<S> sourceList, Class<D> destinationClass) {
        if (sourceList == null) {
            return null;
        }
        return sourceList.stream()
            .map(source -> map(source, destinationClass))
            .collect(Collectors.toList());
    }
}